#!/bin/bash
##############################叶福雄##########################
CLEAR()
{
clear
echo -e "\e[1;32m-------------------------------------------------------\e[0m"
echo -e "\e[1;31m----------欢迎使用常见服务器简单配置脚本------------------\e[0m"
echo -e "\e[1;31m-------------------------------------------------------\e[0m"
echo -e "\e[1;31m-------------------------------------------------------\e[0m"
echo -e "\e[1;31m-----------------------请按提示输入----------------------\e[0m"
}
APACHE()
{
CLEAR
select cii in  "install" "unstall" "makeinstall" "exit"
do
case $cii in
	"install")
		rpm -q httpd && echo "您已经安装apache服务" || yum install -y httpd && echo "安装成功"
                read -p "按回车退出" L
                break
		;;
	"unstall")
		
                        service httpd stop
                        rpm -qc httpd > apache.log
			for t in $(cat apache.log)
                                do
                                        rm -rf $t
                                        rm -f $t
                                done
			rm -rf /var/www//html
			rm -rf /etc/httpd
                        rm -rf apache.log
			yum remove -y httpd &> /dev/null && echo "卸载成功" || echo "未按装"
                        read -p "按回车键退出" L
			break
                        ;;
	"makeinstall")
			MAKE
			;;
	"exit")
			break                                
                        ;;
	*)
		CLEAR
		APACHE
		;;
esac
done
}
MUEN()
{
CLEAR
  echo "1:apache管理"
  echo "2:nfs管理"
  echo "3:dns管理"
  echo "4:ftp管理"
  echo "5:sma管理"
  echo "6:dhcp管理"
  echo "7:PXE环境自动部署"
  echo "8:配置网易yum源"
  echo "9:exit"
}
YUM ()
{
ping -c2 baidu.com -w.2 &> /dev/null
if [ $? -eq 0 ]
	then
	echo -e "[CentOS]\nname=CentOS\nbaseurl=http://mirrors.163.com/centos/6/os/x86_64/\ngpgcheck=0\nenabled=1" > /etc/yum.repos.d/wy.repo
		yum clean all &> /dev/null
		yum makecache &> /dev/null
		MUEN
	else
		echo "network is not"
		break;
fi
}
MAKE()
{
	CLEAR

echo -e "需要下载好apache源码包最好是tar.gz后缀结尾的源码包自动编译安装请与脚本放在同一个目录"

sleep 5
yum install -y gcc gcc-c++ pcre-devel openssl-devel
dir=$(pwd)
daa=$(ls -d httpd* |grep -E "^[httpd]|[0-9]$")
drr=$(find $dir | grep -E "^[httpd]|z$")
ls > tar.log
for i in $(cat tar.log)
	do
		tar xf $i &> /dev/null


	done
sleep 5

mv apr-1.5.1 ${daa}/srclib/apr 
mv apr-util-1.5.4 ${daa}/srclib/apr-util
mv ${drr} /tmp/
cd $daa
if [ -f configure ]
 then
./configure  --prefix=/usr/local/apache2 --sysconfdir=/etc/apache2/conf/ --with-inculded-apr --enable-so --enable-rewirte --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-mpms-shared=all
        echo "正在执行请稍后"
        if [ -f Makefile ]
                then
                        make&& make install
                        echo -e "安装路径:/usr/local/apache2\n配置文件:/etc/apache2/conf\n需要手动启动停止/usr/local/apache/bin/apachectl start/stop"
                else
                        echo "找不到makefile文件"
        fi
else
        echo "找不到configure文件"
        echo "安装失败"

fi
if id apache
	then
		echo "编译安装成功"
	else
		useradd apache
		echo "编译安装成并添加了apache用户"

fi
MUEN
}
#MYSQL()
#{
#
#}

DHCP()
{
CLEAR
select dii in  "install" "setup" "unstall" "exit"
do
case $dii in
	"install")
		rpm -q dhcp && echo "dhcp安装成功" ||yum install -y dhcp
		chkconfig dhcpd on
		;;
	"unstall")	
		service dhcpd stop
                rpm -ql dhcp > dhcp.log
                for o in $(cat dhcp.log)
                    do
                        rm -rf $o
                        rm -f $o
                    done
			rm -rf /etc/dhcp
                        rm -rf dhcp.log
                        yum remove -y dhcp &> /dev/null && echo "卸载成功" || echo "未按装"
                        read -p "按回车退出" L
                        break	
		;;
	"setup")
		DHCP_SETUP
		;;
	"exit")
		break
		;;
	*)
		CLEAR
		DHCP
		;;
esac
done

}
DHCP_SETUP()
{
date=$(date +%F)

cp -p /etc/dhcp/dhcpd.conf /etc/bak/dhcpd_$date.conf
echo "此配置会覆盖掉原有配置，原有配置会备份在/etc/bak/目录"
echo -e "你现在可用的网卡为\n"
ifconfig |awk -F '[ ]+' '{print $1}'|grep -v "lo"|xargs
echo "请输入你要启用的网卡"
read -p ":" et
echo "你选择的是$et网卡请开始配置"
ip=$(ifconfig $et|grep "inet addr"|awk -F: '{print $2}'|awk -F '[ ]+' '{print $1}')
echo -e "你的网卡ip地址为\n$ip\n请配置网卡相同网段才可开启服务"
echo DHCPDARGS=$et > /etc/sysconfig/dhcpd
echo "请输入subnet子网"
read -p ":" subnet
echo "请输入netmask子网掩码"
read -p ":" netmask
echo -e "输入ip地址范围\n如:192.168.10.0 ~ 192.168.10.50范围0～50\n请输入起始ip"
read -p ":" ip
echo -e "输入终止ip"
read -p ":" ipi
echo -e "输入DNS服务器地址\n如114.114.114.114"
read -p ":" dns
echo "定义区域名称（即域名）\n如pk.com"
read -p ":" sname
echo "定义网关ip地址"
read -p ":" gwip
echo "定义广播地址"
read -p ":" gb
cat > /etc/dhcp/dhcpd.conf <<EOF
#ddns-update-style interim;
allow booting;
allow bootp;
subnet $subnet netmask $netmask {
 range $ip $ipi;
 option  domain-name-servers $dns;
 option  domain-name "$sname";
 option  routers $gwip;
 option  broadcast-address $gb;
 default-lease-time  1600;
 max-lease-time  17200;
 }
EOF
echo -e "默认租约时间1600s\n最大租约时间17200s"
service dhcpd restart

}

NFS()
{
CLEAR
select bii in  "install" "setup" "unstall" "exit"
do
        case $bii in
                "install")
                       rpm  -q  nfs-utils  rpcbind && echo "nfs安装成功" ||yum  install -y nfs-utils  rpcbind
			service nfs restart
			chkconfig nfs on
			read -p "按回车退出" L
			break;
                        ;;
                "setup")
                        echo -e "开始配置nfs服务\n\t\t请输入共享目录的绝对路径"
        while :
        do
                        read -p ":" c
                if [ -d "$c" ]
                        then
                                grep -w -q "^$c" /etc/exports
                        if [ $? -eq 0 ]
                                then
                                        echo "你输入的目录已经共享"
                                        cat -n /etc/exports
                                else
                                        echo -e "$c *(ro) " >> /etc/exports
                                        cat -n /etc/exports
					service  rpcbind  restart
					service  nfs  restart
					showmount -e 127.0.0.1
                                        break;
                        fi
                                        break;
                        else
                                echo "你输入的目录路径不正确，请仔细查找目录"
                               
                fi
        done
                                read -p "按回车退出" L
                                break
                                ;;
                "unstall")		service nfs stop
                        		rpm -qc nfs-utils rpcbind > nfs.log
                       			for o in $(cat nfs.log)
                               		 do
                                        	rm -rf $o
                                        	rm -f $o
						rm -f /etc/exposts
                                	done
                        		rm -rf nfs.log
                        		yum remove -y nfs-utils rpcbind &> /dev/null && echo "卸载成功" || echo "未按装"
                        		read -p "按回车退出" L
                                	break

                                ;;
		"exit")  
			  	break                                
				;;
		*)
				CLEAR	
				NFS
				;;
            esac
done 

}
PXE()
{
service  iptables  stop
chkconfig  iptables  off
setenforce  0
getenforce
clear
echo -e "选择自动安装iso的系统版本！只支持redhat系列"
select pxe in  "redhat6" "rehat7" "exit"
do
case $pxe in

	redhat6)  
		echo "正在安装需要的组件........................................................"
		yum  install  -y  dhcp  tftp-server  xinetd  syslinux   vsftpd  ftp  lftp   httpd updatedb &> /dev/null && echo "安装成功开始下一步" || echo "请检查网络后重试"；break
		[  -f  /etc/dhcp/dhcpd.conf ] && echo bak_ok || cp  -v  /etc/dhcpd/dhcpd.conf{,.bak}
		grep  pxelinux.0  /etc/dhcp/dhcpd.conf  && echo  DHCP_PXE_OK||cat  > /etc/dhcp/dhcpd.conf <<EOF
subnet  192.168.10.0  netmask  255.255.255.0  {
  range  192.168.10.20  192.168.10.80;
  option  domain-name-servers  192.168.10.25;
  option  domain-name  "up.com";
  option  routers  192.168.10.25;
  option  broadcast-address  192.168.10.255;
  default-lease-time  1600;
  max-lease-time  17200;
  next-server  192.168.10.25;
  filename  "pxelinux.0";
}
EOF
service   dhcpd  configtest
service   dhcpd   restart
chkconfig  dhcpd  on
lsof  -i  :67
echo -e '继续配置VSFTPD服务...'
sleep 4
service   vsftpd  restart
chkconfig  vsftpd  on
lsof  -i  :21
mkdir  -pv  /var/ftp/iso
mount /dev/cdrom   /var/ftp/iso
echo -e '继续配置TFTP_XINETD服务...'
sleep 4
chkconfig   tftp  on
sed  -i  '/disable/s/yes/no/'  /etc/xinetd.d/tftp
grep  disable   /etc/xinetd.d/tftp
service  xinetd  restart
chkconfig   xinetd  on
lsof  -i  :69
echo -e '继续配置PXE启动文件和启动菜单default文件...'
sleep 4
cd  /var/lib/tftpboot
updatedb
locate  pxelinux.0
cp  -v  /usr/share/syslinux/pxelinux.0   /var/lib/tftpboot/
cp  -v  /var/ftp/iso/isolinux/*    /var/lib/tftpboot/
cd  /var/lib/tftpboot
ls
mkdir  -pv  pxelinux.cfg
cp  -v  isolinux.cfg   pxelinux.cfg/default
cp  -v  /var/lib/tftpboot/pxelinux.cfg/default{,.bak}
chmod  -v  644  /var/lib/tftpboot/pxelinux.cfg/default
cat  >/var/lib/tftpboot/pxelinux.cfg/default <<EOF
default vesamenu.c32
#prompt 1
timeout 600

display boot.msg

menu background splash.jpg
menu title Welcome to Red Hat Enterprise Linux 6.5!
menu color border 0 #ffffffff #00000000
menu color sel 7 #ffffffff #ff000000
menu color title 0 #ffffffff #00000000
menu color tabmsg 0 #ffffffff #00000000
menu color unsel 0 #ffffffff #00000000
menu color hotsel 0 #ff000000 #ffffffff
menu color hotkey 7 #ffffffff #ff000000
menu color scrollbar 0 #ffffffff #00000000

label linux
  menu label ^Install or upgrade an existing system
  menu default
  kernel vmlinuz
  append initrd=initrd.img  ks=ftp://192.168.10.25/ks/mks.cfg  method=ftp://192.168.10.25/iso
label vesa
  menu label Install system with ^basic video driver
  kernel vmlinuz
  append initrd=initrd.img xdriver=vesa nomodeset
label rescue
  menu label ^Rescue installed system
  kernel vmlinuz
  append initrd=initrd.img rescue
label local
  menu label Boot from ^local drive
  localboot 0xffff
label memtest86
  menu label ^Memory test
  kernel memtest
  append -

EOF
cat  /var/lib/tftpboot/pxelinux.cfg/default
echo -e'继续配置Kickstart自动安装方案...'
sleep 4
cd  /root
ls
chmod  -v   755   anaconda-ks.cfg
mkdir  -pv  /var/ftp/ks
cp  -v  anaconda-ks.cfg  /var/ftp/ks/mks.cfg
cat  > /var/ftp/ks/mks.cfg <<EOK
###最小版安装环境所需要的安装包（362个rpm软件包）。
# Kickstart file automatically generated by anaconda.
#version=DEVEL
##系统安装方法，install为全新安装，upgrade为升级安装现有系统。
install
#指定安装文件来源，cdrom从光盘安装，nfs从nfs享服务器，url从ftp或http服务器。
#cdrom
#nfs --server=192.168.10.25 --dir=/var/ftp/iso
url --url="ftp://192.168.10.25/iso"
#url --url="http://192.168.10.25/iso"
##指定语言环境
lang zh_CN.UTF-8
##指定键盘布局
keyboard us
##安装完毕后自动重启。
reboot
##指定客户机网卡IP方式。static静态IP，dhcp为动态获取。
#network --onboot yes --device eth0 --bootproto static --ip 192.168.10.8 --netmask 255.255.255.0 --noipv6
network  --bootproto=dhcp --device=eth0 --onboot=on
##指定root用户密码（加密后的密码）。
rootpw  --iscrypted $6$CuvSnIBuzpYQR0C4$Rcej3WU80a1O6hlH5x11LplKqOnZZbj.It5jhPIoPgiyTXKksUysFrujliy3OIm0pQ1Gf3unx6mbi63fFnoCR1
##定义防火墙开放ssh服务，selinux为enforcing强制策略。
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
selinux –enforcing
##定义时区为亚洲上海。
timezone --utc Asia/Shanghai
##指定bootloader的引导方式。
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
##清除sda硬盘的所有分区。
clearpart --all --drives=sda
#volgroup VolGroup --pesize=4096 pv.008002
#logvol / --fstype=ext4 --name=lv_root --vgname=VolGroup --grow --size=1024 --maxsize=51200
#logvol swap --name=lv_swap --vgname=VolGroup --grow --size=1984 --maxsize=1984
#part /boot --fstype=ext4 --size=500
#part pv.008002 --grow --size=1
##清除mbr引导信息。
zerombr
# Partition clearing information
##清除sda硬盘的所有标签。
clearpart --all --initlabel --drives=sda
# Disk partitioning information
##定义分区。/boot分区500M，swap交换分区1024M，/根分区使用所有的剩余空间。
part /boot --fstype="ext4" --size=500
part swap --fstype="swap" --size=1024
part / --fstype="ext4" --grow --size=1
##安装后执行的脚本。创建tom用户，设置tom和root密码为redhat。
%post
useradd   tom
echo  redhat | passwd  --stdin  tom
echo  redhat | passwd  --stdin  root
IP_ZHU=$(ifconfig  eth0 | awk  'NR==2{print  $2}' | awk  -F:  '{print $2}') 
cat  > /etc/yum.repos.d/ftp.repo <<-EOF
[rhel65]
name=rhel65_ftp_yum
baseurl=ftp://$IP_ZHU/iso
enabled=1
gpgcheck=0
EOF
%end
##最小版安装环境所需要的安装包（362个rpm软件包）。
%packages
@chinese-support
@core
@server-policy
openssh-clients 
%end
EOK
cat  /var/ftp/ks/mks.cfg
		break 
		;;

rehat7)
	;;
exit)
	break
	;;
*)
	break
	;;
esac
done
}

DNS()
{
CLEAR
select x in "install" "setup" "unstall" "exit"
do
        case $x in
                install)
                        rpm -q bind bind-utils &> /dev/null || yum install -y bind bind-utils &> /dev/null && echo "安装已成功"
                        cp -p /etc/named.conf /etc/named.conf.bak
                        sed -i 's/listen-on port 53 { 127.0.0.1; };/listen-on port 53 { any; };/g' /etc/named.conf
			sed -i 's/allow-query     { localhost; };/allow-query     { any; };/g' /etc/named.conf
			read -p "按回车退出" L
			break
			;;
                        
                  setup)
#                        echo "输入区域名称以.com结尾"
#                        read -p ':' com
#
	while  true
		do
		echo "输入区域名称以.com结尾"
    		read  -p  ':' com
egrep -q "$com" /etc/named.rfc1912.zones
        if  [ $? -eq 0 ] 
                then
                        echo "已存在"   
                else

cat >> /etc/named.rfc1912.zones <<EOF
zone "${com}" IN {
         type master;
         file "${com}.zone";
         allow-transfer { none; };
         allow-update { none; };
};
EOF
		echo "已添加"
                break
        fi  
done
                        echo "解析区域文件已配置"
                        cd /var/named/
                        cp -p named.localhost ${com}.zone
                        echo -e "开始设置区域解析文件\n输入管理员邮箱\n请输入正>确的邮箱\n比如:xxxx@qq.com"
                        read -p ":" mail
cat > /var/named/${com}.zone <<EOF
\$TTL 1D
@       IN SOA  ${com}. ${mail}. (
                                        0       ; serial
                                        1D      ; refresh
					1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      ${com}.
${com}.         A       127.0.0.1
dns         A       127.0.0.2
www         A       127.0.0.3
EOF
                        check_ok
                       	service named restart
                        check_ok
                        chkconfig named on
                        echo "开始测试解析是否正常"
                        nslookup ${com} 127.0.0.1
			read -p "按回车键退出" L
                        break
                        ;;
		"unstall")
			
                        service named stop
                        rpm -qc bind > rpm.log
			for i in $(cat rpm.log)
                                do
                                        rm -rf $i
                                        rm -f $i
                                done
			rm -rf /var/named/
                        rm -rf rpm.log
			yum remove -y bind bind-utils &> /dev/null && echo "卸载成功" || echo "未按装"
                        read -p "按回车键退出" L
			break
                        ;;
		"exit")
			break                                
                        ;;
		*)
			CLEAR
			DNS
			;;

        esac
done
}
mkdir /etc/bak/
while :
do
MUEN
read -p ":" si
case $si in
	1)
		APACHE
		;;
	2)
		NFS	
		;;
	3)
		DNS
		;;
	4)
		
		;;
	5)
		;;

	6)
		DHCP
		;;
	7)
		PXE
		;;
	8)
		YUM
		;;
	9)
		echo "程序退出"
		break
		;;
	
	*)	
		echo "欢迎使用本脚本喜欢的话多多支持"
		CLEAR
		;;

esac
done
